import 'package:demo202112/utils/common_appbar.dart';
import 'package:flutter/material.dart';

/// @Author wywinstonwy
/// @Date 2022/1/25 8:42 上午
/// @Description:

class WyResponsiveColumn extends StatelessWidget {
  final List<Widget>children;
  const WyResponsiveColumn({Key? key,required this.children}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context,BoxConstraints constraints){
      if(constraints.maxWidth<200){
        // 最大宽度小于200，显示单列
        return Column(children: children,mainAxisSize: MainAxisSize.min,);
      }else{
        // 大于200，显示双列
        var _children =<Widget>[];
        for(var i=0;i<children.length;i+=2){
          if(i+1<children.length){
            _children.add(Row(children: [children[i],children[i+1]],
              mainAxisSize: MainAxisSize.min,
            ));
          }else{
            _children.add(children[i]);
          }
        }
        return Column(children: _children,mainAxisSize: MainAxisSize.min,);
      }
    });
  }
}

class WyLayoutBuilderPage extends StatefulWidget {
  const WyLayoutBuilderPage({Key? key}) : super(key: key);

  @override
  _WyLayoutBuilderPageState createState() => _WyLayoutBuilderPageState();
}

class _WyLayoutBuilderPageState extends State<WyLayoutBuilderPage> {
  var _children = List.filled(6, Text('V'));
  // Column在本示例中在水平方向的最大宽度为屏幕的宽度
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar('LayoutBuilder'),
      body: Column(children: [
        // 限制宽度为190，小于 200
        SizedBox(width: 190, child: WyResponsiveColumn(children: _children)),
        WyResponsiveColumn(children: _children),
        LayoutLogPrint(child:Text("xx")) // 下面介绍
      ],),

    );
  }
}

class LayoutLogPrint <T>extends StatelessWidget {
  final Widget child;
  final T? tag;
  const LayoutLogPrint({Key? key, required this.child, this.tag}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (_,constraints){
      // assert在编译release版本时会被去除
      assert(() {
        print('${tag ?? key ?? child}: $constraints');
        return true;
      }());
      return child;
    });
  }
}


